package com.hsd.znsh.controller.manager;

import com.hsd.znsh.entity.Host;
import com.hsd.znsh.entity.User;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.enums.OnlineStatusEnum;
import com.hsd.znsh.enums.SensorGroupTypeEnum;
import com.hsd.znsh.enums.UserRoleEnum;
import com.hsd.znsh.pojo.output.HostVO;
import com.hsd.znsh.pojo.output.ResultVO;
import com.hsd.znsh.repository.HostRepository;
import com.hsd.znsh.service.HostService;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-25 09:49:38
 */
@RestController
@RequestMapping("/manager/host")
public class ManagerHostController {

    @Autowired
    private HostService hostService;

    @Autowired
    private HostRepository hostRepository;

    @GetMapping("/getOnlineStatusList")
    public ResultVO getSensorGroupTypeList(){
        List<Map<String,Object>> list= EnumUtil.getEnumInfo(OnlineStatusEnum.class,"code","value");
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getAllHostList")
    public ResultVO getHostList() {
        List<Host> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=hostService.getAllHostList(null);
        }else{
            list=hostService.getAllHostList(SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getHostList")
    public ResultVO getHostList(Host host,Integer size) {
        long totalNum;
        host.setDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=hostService.getHostListTotalPages(host,null);
        }else{
            totalNum=hostService.getHostListTotalPages(host,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getHostList")
    public ResultVO getHostList(@PageableDefault(sort = "id",direction = Sort.Direction.ASC)Pageable pageable, Host host) {
        List<HostVO> list;
        host.setDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=hostService.getHostList(pageable,host,null);
        }else{
            list=hostService.getHostList(pageable,host,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getHostInfo")
    public ResultVO getHostInfo(Integer id) {
        HostVO hostVO=hostService.getHostInfo(id);
        return ResultVOUtil.success(hostVO);
    }

    @PostMapping("/editHost")
    public ResultVO editHost(@Valid Host host, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        if(host.getId()==null){
            hostService.addHost(host);
            return ResultVOUtil.success("添加成功");
        }else{
            hostService.updateHost(host);
            return ResultVOUtil.success("修改成功");
        }
    }

    @GetMapping("/deletedHost")
    public ResultVO deletedHost(Integer id) {
        hostService.deletedHost(id);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping(value = "getHostListByName")
    @ResponseBody
    public List<Host> getHostListByName(String hostName) {
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            return hostRepository.findByHostNameLikeAndDeleted("%"+hostName+"%",DeletedStatusEnum.NOT_DELETED.getCode());
        }else{
            return hostRepository.findByManagerIdAndHostNameLikeAndDeleted(
                    SecurityUtil.getUserId(),
                    "%"+hostName+"%",
                    DeletedStatusEnum.NOT_DELETED.getCode());
        }
    }
}
